package newsrecommendation.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import newsrecommendation.datamodel.NewsProduct;
import newsrecommendation.datamodel.utils.Scores;
import newsrecommendation.datamodel.UserProfile;
import newsrecommendation.datamodel.utils.UserType;
import newsrecommendation.utils.StringUtils;

/**
 *
 * @author Andrea
 */
public class DataLoader {

    private static DataLoader instance;
    private static List<String> allKeywords;
    private List<NewsProduct> news_notObligatory;
    private List<NewsProduct> news_obligatory;
    HashMap<UserType, List<Double>> demographicStatistics;
    List<UserProfile> users;
    
    String path;

    private DataLoader() {
    }
    
    private DataLoader(String path){
    	this.path=path;
    }
    
    

    public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public void loadData() {
        loadKeywords(); //Must be first
        //loadNews();
        loadDemographicStatistics(); //Important - Before loading users
        loadUsers();
    }

    public static DataLoader get() {
        if (instance == null) {
            instance = new DataLoader();
        }
        return instance;
    }

    public List<String> getAllKeywords() {
        return allKeywords;
    }

    public HashMap<UserType, List<Double>> getDemographicStatistics() {
        return demographicStatistics;
    }

    public List<NewsProduct> getNews_notObligatory() {
        return news_notObligatory;
    }

    public List<NewsProduct> getNews_obligatory() {
        return news_obligatory;
    }

    public List<UserProfile> getUsers() {
        return users;
    }

    private void loadKeywords() {
        allKeywords = new ArrayList();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream((path==null?"":path)+"input\\keywordPositions.txt"), "UTF-8"));
            String keyword="";
            while ((keyword = br.readLine()) != null) {
            	keyword = StringUtils.preprocess(keyword);
                
                allKeywords.add(keyword);
            }
        } catch (Exception ex) {
            //TODO - Correctly handle exception
            System.err.println("Keywords file could not be loaded.");
        }
        
       // System.out.println(allKeywords);
    }

    private void loadDemographicStatistics() {
        demographicStatistics = new HashMap();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream((path==null?"":path)+"input\\demographic.txt"), "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] info = line.split(";");
                UserType key = getUserType(info[0]);
                String[] interests = info[1].split(",");
                List<Double> values = new ArrayList();
                for (int i = 0; i < interests.length; i++) {
                    values.add(Double.valueOf(Math.round((Double.valueOf(interests[i])) * Scores.INTEREST)) / 100);
                }
                demographicStatistics.put(key, values);
            }
        } catch (Exception ex) {
            //TODO - Correctly handle exception
            System.err.println("Demographic file could not be loaded.");
        }
    }

    private void loadNews() {
        news_notObligatory = new ArrayList();
        news_obligatory = new ArrayList();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream((path==null?"":path)+"input\\news.txt"), "UTF-8"));
            String line;
            int newsId = 1;
            while ((line = br.readLine()) != null) {
                String[] info = line.split(";");
                String keywords = info[1];
                if (StringUtils.preprocess(keywords).contains(StringUtils.preprocess("Saludo del Rector"))) {
                    news_obligatory.add(new NewsProduct(String.valueOf(newsId), info[0], keywords));
                } else {
                    news_notObligatory.add(new NewsProduct(String.valueOf(newsId), info[0], keywords));
                }
                newsId++;
            }
        } catch (Exception ex) {
            //TODO - Correctly handle exception
            System.err.println("News file could not be loaded.");
        }
    }

    private void loadUsers() {
        users = new ArrayList();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream((path==null?"":path)+"input\\users.txt"), "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] info = line.split(";");
                UserProfile newUser = new UserProfile(String.valueOf(info[0]), getUserType(info[1]));
                if (info.length == 3) {
                    newUser.setInterestsPosition(info[2]);
                }
                users.add(newUser);
            }
        } catch (Exception ex) {
            //TODO - Correctly handle exception
            System.err.println("User file could not be loaded.");
        }
    }

    private UserType getUserType(String userType) {
        if (StringUtils.preprocess(userType).contains("estudiante")) {
            return UserType.ESTUDIANTE;
        } else if (StringUtils.preprocess(userType).contains("egresado")) {
            return UserType.EGRESADO;
        } else if (StringUtils.preprocess(userType).contains("empleado")) {
            return UserType.EMPLEADO;
        }
        if (StringUtils.preprocess(userType).contains("jesuita")) {
            return UserType.JESUITA;
        } else if (StringUtils.preprocess(userType).contains("profesorcatedra")) {
            return UserType.PROFESOR_CATEDRA;
        }
        if (StringUtils.preprocess(userType).contains("profesorplanta")) {
            return UserType.PROFESOR_PLANTA;
        }else if (StringUtils.preprocess(userType).contains("comunidad")) {
            return UserType.COMUNIDAD;
        }
        return null;
    }
}
